using System;
using System.Collections.Generic;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for UIGridInfo objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for UIGridInfo.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestUIGridInfo
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            base.SetupFixture();
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Ignore("Need to implement ToString")] //TODO Brett 24 Feb 2009: Ignored Test - Need to implement ToString
        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            //---------------Test Result -----------------------
            Assert.Fail("Implement ToString() for the Business object and implement this test");
        }

        [Test]
        public void Test_InsertNewUIGridInfo()
        {
            //---------------Set up test pack-------------------
            UIGridInfo uiGridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiGridInfo.Status.IsNew);

            //---------------Execute Test ----------------------
            uiGridInfo.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(uiGridInfo.Status.IsNew);
        }

        [Test]
        public void Test_DeleteUIGridInfo_MarksAsDeleted()
        {
            //---------------Set up test pack-------------------
            UIGridInfo uiGridInfo = TestUtilsShared.CreateSavedUIGridInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiGridInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiGridInfo.IsDeletable(out message));

            //---------------Execute Test ----------------------
            uiGridInfo.MarkForDelete();
            uiGridInfo.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(uiGridInfo.Status.IsNew);
            Assert.IsTrue(uiGridInfo.Status.IsDeleted);
        }

        [Test]
        public void Test_UpdateUIGridInfo_DoesNotGiveError()
        {
            //---------------Set up test pack-------------------
            UIGridInfo uiGridInfo = TestUtilsShared.CreateSavedUIGridInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiGridInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiGridInfo.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiGridInfo.SortColumn = "Test Form";

            //---------------Test Result -----------------------
            Assert.IsFalse(uiGridInfo.Status.IsNew);
        }

        [Test]
        public void Test_ICollection_Count()
        {
            //---------------Set up test pack-------------------
            UIGridColumnInfo gridColumnInfo = TestUtilsShared.CreateUnsavedValidUIColumnInfo();
            UIGridInfo gridInfo = gridColumnInfo.UIGrid;
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(gridInfo);
            Assert.AreEqual(1, gridInfo.UIColumns.Count);
            //---------------Execute Test ----------------------
            int count = grid.Count;
            //---------------Test Result -----------------------
            Assert.AreEqual(gridInfo.UIColumns.Count, count);
        }

        [Test]
        public void Test_IUIGrid_Contains_ReturnsTrue()
        {
            //---------------Set up test pack-------------------
            UIGridColumnInfo gridColumnInfo = TestUtilsShared.CreateUnsavedValidUIColumnInfo();
            UIGridInfo gridInfo = gridColumnInfo.UIGrid;
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(gridInfo);
            Assert.IsTrue(gridInfo.UIColumns.Contains(gridColumnInfo));
            //---------------Execute Test ----------------------
            bool contains = grid.Contains(gridColumnInfo);
            //---------------Test Result -----------------------
            Assert.IsTrue(contains); ;
        }

        [Test]
        public void Test_IUIGrid_Contains_ReturnsFalse()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            IUIGrid grid = gridInfo;
            UIGridColumnInfo gridColumnInfo = new UIGridColumnInfo();
            //---------------Assert Precondition----------------
            Assert.IsFalse(gridInfo.UIColumns.Contains(gridColumnInfo));
            //---------------Execute Test ----------------------
            bool contains = grid.Contains(gridColumnInfo);

            //---------------Test Result -----------------------
            Assert.IsFalse(contains);
        }

        [Test]
        public void Test_IUIGrid_this()
        {
            //---------------Set up test pack-------------------
            UIGridColumnInfo gridColumnInfo1 = TestUtilsShared.CreateUnsavedValidUIColumnInfo();
            UIGridInfo gridInfo = gridColumnInfo1.UIGrid;
            UIGridColumnInfo gridColumnInfo2 = TestUtilsShared.CreateUnsavedValidUIColumnInfo(gridInfo);
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, gridInfo.UIColumns.Count);
            //---------------Execute Test ----------------------
            IUIGridColumn gridColumn = grid[1];
            //---------------Test Result -----------------------
            Assert.IsNotNull(gridColumn);
            Assert.AreSame(gridColumnInfo2, gridColumn);
        }


        [Test]
        public void Test_IUIGrid_SortColumn_Get()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            gridInfo.SortColumn = TestUtilsShared.GetRandomString();
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string sortColumn = grid.SortColumn;
            //---------------Test Result -----------------------
            Assert.AreEqual(gridInfo.SortColumn, sortColumn);
        }

        [Test]
        public void Test_IUIGrid_SortColumn_Set()
        {
            //---------------Set up test pack-------------------
            string sortColumn = TestUtilsShared.GetRandomString();
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(sortColumn, gridInfo.SortColumn);
            //---------------Execute Test ----------------------
            grid.SortColumn = sortColumn;
            //---------------Test Result -----------------------
            Assert.AreEqual(sortColumn, gridInfo.SortColumn);
        }

        [Test]
        public void Test_IUIGrid_FilterDef_Get()
        {
            //---------------Set up test pack-------------------
            UIGridFilter gridFilter = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridInfo gridInfo = gridFilter.UIGridInfo;
            IUIGrid grid = gridInfo;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IFilterDef filterDef = grid.FilterDef;
            //---------------Test Result -----------------------
            Assert.AreSame(gridFilter, filterDef);
        }

        [Test]
        public void Test_IUIGrid_FilterDef_Set()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            IUIGrid grid = gridInfo;
            UIGridFilter gridFilter = new UIGridFilter();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            grid.FilterDef = gridFilter;
            //---------------Test Result -----------------------
            Assert.AreSame(grid.FilterDef, gridFilter);

        }
/*        [Test]
        public void Test_IUIGrid_UIDef_GetAndSet()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            IUIGrid grid = gridInfo;
            UIView uiView = new UIView();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            grid.UIDef = uiView;
            //---------------Test Result -----------------------
            Assert.AreSame(grid.UIDef, uiView);

        }*/

        [Test]
        public void Test_CreateNewUIFields_ShouldSetOrdinals()
        {
            //---------------Set up test pack-------------------
            UIGridInfo uiGridInfo = new UIGridInfo();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            UIGridColumnInfo gridColumnInfo1 = uiGridInfo.UIColumns.CreateBusinessObject();
            UIGridColumnInfo gridColumnInfo2 = uiGridInfo.UIColumns.CreateBusinessObject();
            UIGridColumnInfo gridColumnInfo3 = uiGridInfo.UIColumns.CreateBusinessObject();
            UIGridColumnInfo gridColumnInfo4 = uiGridInfo.UIColumns.CreateBusinessObject();
            UIGridColumnInfo gridColumnInfo5 = uiGridInfo.UIColumns.CreateBusinessObject();
            UIGridColumnInfo gridColumnInfo6 = uiGridInfo.UIColumns.CreateBusinessObject();
            //---------------Test Result -----------------------
            TestUtilsShared.AssertItemsAreInOrder(gridColumnInfo1, gridColumnInfo2, gridColumnInfo3, gridColumnInfo4, gridColumnInfo5, gridColumnInfo6);
        }
  [Test]
        public void Test_GetParent()
        {
            //---------------Set up test pack-------------------
            UIView view = new UIView();
            UIGridInfo gridInfo = new UIGridInfo();
            view.UIGridInfo = gridInfo;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IModelElement parent = ((IModelElement) gridInfo).Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(view, parent);
        }

        [Test]
        public void Test_SetParentExceptionThrown()
        {
            //---------------Set up test pack-------------------
            IModelElement prop = new UIGridInfo();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Exception exceptionThrown = null;
            try
            {
                prop.Parent = new UIView();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf(typeof (HabaneroDeveloperException), exceptionThrown);
            StringAssert.Contains
                ("Set the parent of the UIGridInfo by setting the related UIView object", exceptionThrown.Message);
        }
    
    }
}